{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "529eb7e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.linear_model import LogisticRegression as LR  #逻辑回归模型方法\n",
    "from sklearn.model_selection import train_test_split   #分组方法，切分数据集\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.datasets import load_breast_cancer #导入数据 内带癌症数据集为例\n",
    "from sklearn.preprocessing import MinMaxScaler as mms  #归一化去量纲工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "68d79cf5",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_01 = load_breast_cancer().data\n",
    "Y = load_breast_cancer().target\n",
    "mms_01 = mms().fit(X_01)\n",
    "X = mms_01.transform(X_01) #归一化处理\n",
    "data = pd.DataFrame(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "97821fc9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.968365553602812, 0.9595782073813708)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr2 = LR(penalty='l2',solver='liblinear',C=0.5,max_iter=1000)  #实例化\n",
    "lr1 = LR(penalty='l1',solver='liblinear',C=0.5,max_iter=1000)  #实例化\n",
    "lr2.fit(X,Y) #训练模型\n",
    "lr1.fit(X,Y) #训练模型\n",
    "score_2 = lr2.score(X,Y) #模型得分\n",
    "score_1 = lr1.score(X,Y) #模型得分\n",
    "score_1,score_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6c470c56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.70097945, -0.60195705, -0.76546043, -0.93922794,  0.342238  ,\n",
       "        -0.49093433, -1.53614524, -2.01686153,  0.28618506,  1.18768139,\n",
       "        -0.93999684,  0.37736325, -0.74223063, -0.69438744,  0.39193797,\n",
       "         0.43785105,  0.26273008,  0.12574924,  0.45815422,  0.48960182,\n",
       "        -1.39452597, -1.05167756, -1.32083088, -1.25390102, -0.48670202,\n",
       "        -0.74954984, -1.13936202, -2.00200264, -0.58785718, -0.16992221]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr2.coef_  #w的参数值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4bf6d20b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.99139414, 0.00860586],\n",
       "       [0.8996178 , 0.1003822 ],\n",
       "       [0.97791871, 0.02208129],\n",
       "       ...,\n",
       "       [0.72616515, 0.27383485],\n",
       "       [0.99828799, 0.00171201],\n",
       "       [0.02452444, 0.97547556]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr1.coef_  #w的参数值，可以对特征进行筛选，防止过拟合\n",
    "lr2.predict_proba(X) #对每一个X进行预测，判断在二分类中各部分的概率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "72e51476",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAFmCAYAAAB9fWHDAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAuUElEQVR4nO3deXCcd53v+/e3W7u1xWpZXuQtjtckjiFOSEggG0lMEpxigFPJnUCx1OSeuTBzOWdgijlzC6YYqDMUdzJAwb3n5oAnS9VAfOAM5TNkyDBYIZDExA5x7Fi2bMdLLMu21G3tu7q/94+WFXlV22qp1c/zeVWp0sujp7+PYn/80297zN0REZH8F8l1ASIikh0KdBGRgFCgi4gEhAJdRCQgFOgiIgGhQBcRCYgJA93MNplZq5m9d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WOWYWI90Fc2gaa5xKmVz/O8A9AGa2mnSgh+lec1uAT43OdrkF6HT3E5M5obpcpoi7j5jZF4AXSI/4b3L3PWb2dWCHu28Bvg2UA//DzADecfeNOSs6izK8/sDK8PpfAO4zs0YgCXzZ3RO5qzp7Mrz+vwD+u5n9J9IDpJ/20ekfQWBmPyb9D3ZsdJzga0AhgLv/N9LjBg8AB4E+4DOT/swA/fxEREJNXS4iIgGhQBcRCQgFuohIQCjQRUQCQoEuIhIQCnQRkYBQoIuIBMT/D/nbRvJif+82AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "l1 = []\n",
    "l2 = []\n",
    "l1test = []\n",
    "l2test = []\n",
    "#切分数据集\n",
    "Xtrain,Xtest,Ytrain,Ytest = train_test_split(X,Y,test_size = 0.3,random_state=420)\n",
    "#测试C在l1，l2中的表现\n",
    "for i in np.linspace(0.05,1,19): #linspace均分计算指令\n",
    "    #训练集\n",
    "    lrl2 = LR(penalty='l2',solver='liblinear',C=i,max_iter=1000).fit(Xtrain,Ytrain)  #实例化\n",
    "    lrl1 = LR(penalty='l1',solver='liblinear',C=i,max_iter=1000).fit(Xtrain,Ytrain)  #实例化 \n",
    "    l1.append(lrl1.score(Xtrain,Ytrain))\n",
    "    l2.append(lrl2.score(Xtrain,Ytrain))\n",
    "    #测试集\n",
    "    lrl2 = LR(penalty='l2',solver='liblinear',C=i,max_iter=1000).fit(Xtest,Ytest)  #实例化\n",
    "    lrl1 = LR(penalty='l1',solver='liblinear',C=i,max_iter=1000).fit(Xtest,Ytest)  #实例化 \n",
    "    l1test.append(accuracy_score(lrl1.predict(Xtest),Ytest))\n",
    "    l2test.append(accuracy_score(lrl2.predict(Xtest),Ytest))\n",
    "\n",
    "graph = [l1,l2,l1test,l2test]\n",
    "color = ['green','black','lightgreen','gray']\n",
    "label = ['l1','l2','l1test','l2test']\n",
    "\n",
    "plt.figure(figsize=(6,6))\n",
    "for i in range(len(graph)):\n",
    "    plt.plot(np.linspace(0.05,1,19),graph[i],color[i],label=label[i])\n",
    "plt.legend(loc=4) #图例的位置\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "64f0dd85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mean radius</th>\n",
       "      <th>mean texture</th>\n",
       "      <th>mean perimeter</th>\n",
       "      <th>mean area</th>\n",
       "      <th>mean smoothness</th>\n",
       "      <th>mean compactness</th>\n",
       "      <th>mean concavity</th>\n",
       "      <th>mean concave points</th>\n",
       "      <th>mean symmetry</th>\n",
       "      <th>mean fractal dimension</th>\n",
       "      <th>...</th>\n",
       "      <th>worst texture</th>\n",
       "      <th>worst perimeter</th>\n",
       "      <th>worst area</th>\n",
       "      <th>worst smoothness</th>\n",
       "      <th>worst compactness</th>\n",
       "      <th>worst concavity</th>\n",
       "      <th>worst concave points</th>\n",
       "      <th>worst symmetry</th>\n",
       "      <th>worst fractal dimension</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>17.99</td>\n",
       "      <td>10.38</td>\n",
       "      <td>122.80</td>\n",
       "      <td>1001.0</td>\n",
       "      <td>0.11840</td>\n",
       "      <td>0.27760</td>\n",
       "      <td>0.3001</td>\n",
       "      <td>0.14710</td>\n",
       "      <td>0.2419</td>\n",
       "      <td>0.07871</td>\n",
       "      <td>...</td>\n",
       "      <td>17.33</td>\n",
       "      <td>184.60</td>\n",
       "      <td>2019.0</td>\n",
       "      <td>0.1622</td>\n",
       "      <td>0.6656</td>\n",
       "      <td>0.7119</td>\n",
       "      <td>0.2654</td>\n",
       "      <td>0.4601</td>\n",
       "      <td>0.11890</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20.57</td>\n",
       "      <td>17.77</td>\n",
       "      <td>132.90</td>\n",
       "      <td>1326.0</td>\n",
       "      <td>0.08474</td>\n",
       "      <td>0.07864</td>\n",
       "      <td>0.0869</td>\n",
       "      <td>0.07017</td>\n",
       "      <td>0.1812</td>\n",
       "      <td>0.05667</td>\n",
       "      <td>...</td>\n",
       "      <td>23.41</td>\n",
       "      <td>158.80</td>\n",
       "      <td>1956.0</td>\n",
       "      <td>0.1238</td>\n",
       "      <td>0.1866</td>\n",
       "      <td>0.2416</td>\n",
       "      <td>0.1860</td>\n",
       "      <td>0.2750</td>\n",
       "      <td>0.08902</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>19.69</td>\n",
       "      <td>21.25</td>\n",
       "      <td>130.00</td>\n",
       "      <td>1203.0</td>\n",
       "      <td>0.10960</td>\n",
       "      <td>0.15990</td>\n",
       "      <td>0.1974</td>\n",
       "      <td>0.12790</td>\n",
       "      <td>0.2069</td>\n",
       "      <td>0.05999</td>\n",
       "      <td>...</td>\n",
       "      <td>25.53</td>\n",
       "      <td>152.50</td>\n",
       "      <td>1709.0</td>\n",
       "      <td>0.1444</td>\n",
       "      <td>0.4245</td>\n",
       "      <td>0.4504</td>\n",
       "      <td>0.2430</td>\n",
       "      <td>0.3613</td>\n",
       "      <td>0.08758</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11.42</td>\n",
       "      <td>20.38</td>\n",
       "      <td>77.58</td>\n",
       "      <td>386.1</td>\n",
       "      <td>0.14250</td>\n",
       "      <td>0.28390</td>\n",
       "      <td>0.2414</td>\n",
       "      <td>0.10520</td>\n",
       "      <td>0.2597</td>\n",
       "      <td>0.09744</td>\n",
       "      <td>...</td>\n",
       "      <td>26.50</td>\n",
       "      <td>98.87</td>\n",
       "      <td>567.7</td>\n",
       "      <td>0.2098</td>\n",
       "      <td>0.8663</td>\n",
       "      <td>0.6869</td>\n",
       "      <td>0.2575</td>\n",
       "      <td>0.6638</td>\n",
       "      <td>0.17300</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>20.29</td>\n",
       "      <td>14.34</td>\n",
       "      <td>135.10</td>\n",
       "      <td>1297.0</td>\n",
       "      <td>0.10030</td>\n",
       "      <td>0.13280</td>\n",
       "      <td>0.1980</td>\n",
       "      <td>0.10430</td>\n",
       "      <td>0.1809</td>\n",
       "      <td>0.05883</td>\n",
       "      <td>...</td>\n",
       "      <td>16.67</td>\n",
       "      <td>152.20</td>\n",
       "      <td>1575.0</td>\n",
       "      <td>0.1374</td>\n",
       "      <td>0.2050</td>\n",
       "      <td>0.4000</td>\n",
       "      <td>0.1625</td>\n",
       "      <td>0.2364</td>\n",
       "      <td>0.07678</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 31 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   mean radius  mean texture  mean perimeter  mean area  mean smoothness  \\\n",
       "0        17.99         10.38          122.80     1001.0          0.11840   \n",
       "1        20.57         17.77          132.90     1326.0          0.08474   \n",
       "2        19.69         21.25          130.00     1203.0          0.10960   \n",
       "3        11.42         20.38           77.58      386.1          0.14250   \n",
       "4        20.29         14.34          135.10     1297.0          0.10030   \n",
       "\n",
       "   mean compactness  mean concavity  mean concave points  mean symmetry  \\\n",
       "0           0.27760          0.3001              0.14710         0.2419   \n",
       "1           0.07864          0.0869              0.07017         0.1812   \n",
       "2           0.15990          0.1974              0.12790         0.2069   \n",
       "3           0.28390          0.2414              0.10520         0.2597   \n",
       "4           0.13280          0.1980              0.10430         0.1809   \n",
       "\n",
       "   mean fractal dimension  ...  worst texture  worst perimeter  worst area  \\\n",
       "0                 0.07871  ...          17.33           184.60      2019.0   \n",
       "1                 0.05667  ...          23.41           158.80      1956.0   \n",
       "2                 0.05999  ...          25.53           152.50      1709.0   \n",
       "3                 0.09744  ...          26.50            98.87       567.7   \n",
       "4                 0.05883  ...          16.67           152.20      1575.0   \n",
       "\n",
       "   worst smoothness  worst compactness  worst concavity  worst concave points  \\\n",
       "0            0.1622             0.6656           0.7119                0.2654   \n",
       "1            0.1238             0.1866           0.2416                0.1860   \n",
       "2            0.1444             0.4245           0.4504                0.2430   \n",
       "3            0.2098             0.8663           0.6869                0.2575   \n",
       "4            0.1374             0.2050           0.4000                0.1625   \n",
       "\n",
       "   worst symmetry  worst fractal dimension  label  \n",
       "0          0.4601                  0.11890      0  \n",
       "1          0.2750                  0.08902      0  \n",
       "2          0.3613                  0.08758      0  \n",
       "3          0.6638                  0.17300      0  \n",
       "4          0.2364                  0.07678      0  \n",
       "\n",
       "[5 rows x 31 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.model_selection import GridSearchCV #网格搜索\n",
    "from sklearn.preprocessing import StandardScaler #标准化-去量纲\n",
    "#数据处理\n",
    "data = pd.DataFrame(X_01,columns=load_breast_cancer().feature_names)\n",
    "data['label'] = Y\n",
    "data.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9075018d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\LouisLou\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\sklearn\\model_selection\\_validation.py:372: FitFailedWarning: \n",
      "95 fits failed out of a total of 380.\n",
      "The score on these train-test partitions for these parameters will be set to nan.\n",
      "If these failures are not expected, you can try to debug them by setting error_score='raise'.\n",
      "\n",
      "Below are more details about the failures:\n",
      "--------------------------------------------------------------------------------\n",
      "95 fits failed with the following error:\n",
      "Traceback (most recent call last):\n",
      "  File \"C:\\Users\\LouisLou\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\sklearn\\model_selection\\_validation.py\", line 680, in _fit_and_score\n",
      "    estimator.fit(X_train, y_train, **fit_params)\n",
      "  File \"C:\\Users\\LouisLou\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\sklearn\\linear_model\\_logistic.py\", line 1461, in fit\n",
      "    solver = _check_solver(self.solver, self.penalty, self.dual)\n",
      "  File \"C:\\Users\\LouisLou\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\sklearn\\linear_model\\_logistic.py\", line 434, in _check_solver\n",
      "    raise ValueError(\n",
      "ValueError: Logistic Regression supports only solvers in ['liblinear', 'newton-cg', 'lbfgs', 'sag', 'saga'], got Ibfgs.\n",
      "\n",
      "  warnings.warn(some_fits_failed_message, FitFailedWarning)\n",
      "C:\\Users\\LouisLou\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\sklearn\\model_selection\\_search.py:969: UserWarning: One or more of the test scores are non-finite: [0.97240506        nan 0.97990506 0.97240506 0.97740506        nan\n",
      " 0.98743671 0.97740506 0.98243671        nan 0.98743671 0.98243671\n",
      " 0.98243671        nan 0.98743671 0.98243671 0.98493671        nan\n",
      " 0.98493671 0.98493671 0.98746835        nan 0.98493671 0.98746835\n",
      " 0.98746835        nan 0.98493671 0.98746835 0.98493671        nan\n",
      " 0.98493671 0.98493671 0.98493671        nan 0.98493671 0.98493671\n",
      " 0.98493671        nan 0.98493671 0.98493671 0.98493671        nan\n",
      " 0.98240506 0.98493671 0.98493671        nan 0.98240506 0.98493671\n",
      " 0.98493671        nan 0.98240506 0.98493671 0.98240506        nan\n",
      " 0.98240506 0.98240506 0.98240506        nan 0.98240506 0.98240506\n",
      " 0.98240506        nan 0.98240506 0.98240506 0.98240506        nan\n",
      " 0.98240506 0.98240506 0.97987342        nan 0.98240506 0.97987342\n",
      " 0.97987342        nan 0.98240506 0.97987342]\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'C': 0.3138888888888889, 'solver': 'newton-cg'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#切分数据集\n",
    "Xtrain,Xtest,Ytrain,Ytest = train_test_split(X,Y,test_size = 0.3,random_state=420)\n",
    "#标准化\n",
    "Xtrain_ = (StandardScaler().fit(Xtrain)).transform(Xtrain)\n",
    "Xtest_ = (StandardScaler().fit(Xtrain)).transform(Xtest)\n",
    "#网格搜索\n",
    "p = {\n",
    "    'C':list(np.linspace(0.05,1,19)),\n",
    "    'solver':['newton-cg','Ibfgs','liblinear','sag']\n",
    "\n",
    "}\n",
    "\n",
    "model = LR(penalty='l2',max_iter=1000)\n",
    "GS = GridSearchCV(model,p,cv=5)\n",
    "\n",
    "GS.fit(Xtrain_,Ytrain)\n",
    "GS.best_score_\n",
    "#结果：0.9874683544303797\n",
    "#确定最优参数\n",
    "GS.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5f58576e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.9824561403508771, 0.9874371859296482)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lrl2 = LR(penalty='l2',\n",
    "  C=GS.best_params_['C'],\n",
    "  solver=GS.best_params_['solver'],\n",
    "  max_iter=1000)\n",
    "lrl2.fit(Xtrain_,Ytrain)\n",
    "score22 = lrl2.score(Xtrain_,Ytrain)\n",
    "lrl2.fit(Xtest_,Ytest)\n",
    "score11 = lrl2.score(Xtest_,Ytest)\n",
    "\n",
    "score11,score22"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8aa07fd8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
